package com.pug.config.mvc;

import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import javax.annotation.Resource;

/**
 * @author 飞哥yykk
 * 更多学习关注飞哥B站
 * 地址是：https://space.bilibili.com/490711252
 * @title: WebMvcConfiguration
 * @projectName ksd-user-course-center
 * @description: TODO
 * @date 2021/9/2622:47
 */
@Configuration
public class WebMvcConfiguration<PassportTokenInterceptor, PassportInterceptor> implements WebMvcConfigurer {

    @Resource
    public PassportTokenInterceptor passportTokenInterceptor;



    @Resource
    private PassportInterceptor passportInterceptor;

    /**
     * 注册拦截器
     *
     * @param registry
     */
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        // token 拦截 带api路径的都需要携带token
        registry.addInterceptor((HandlerInterceptor) passportTokenInterceptor).addPathPatterns("/api/**");
    }

    /**
     * 解决跨域问题
     *
     * @param registry
     */
    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry
                .addMapping("/**")
                // 允许跨域的域名
                .allowedOriginPatterns("*") // 允许所有域
                .allowedMethods("POST", "GET", "PUT", "OPTIONS", "DELETE")
                .allowedHeaders("*") // 允许任何请求头
                .allowCredentials(true) // 允许证书、cookie
                .maxAge(3600L); // maxAge(3600)表明在3600秒内，不需要再发送预检验请求，可以缓存该结果
    }
}
